from quantification.strategy.indicator.Indicator import IndicatorHandler
import pandas as pd


class MAIndicators(IndicatorHandler):
    '''
    ma均线指标
    '''

    def __init__(self, indicatorHandler: IndicatorHandler = None):
        self.next = indicatorHandler

    def handle(self, pd_data):
        '''
        ma处理（金叉，死叉信号，其他指标）
        :param pd_data:
        :return:
        '''
        self.volume_analysis(pd_data)
        if self.next:
            self.next.handle(pd_data)

    def golden_dead_analysis(self, pd_data, short_window=5, long_window=10):
        '''
        金叉，死叉
        :param pd_data:
        :param period:
        :return:
        '''
        pd_data['ma_short'] = pd_data['close'].rolling(short_window).mean()
        pd_data['ma_long'] = pd_data['close'].rolling(long_window).mean()

        # 金叉
        pd_data.loc[(pd_data['ma_short'] >= pd_data['ma_long']) & (
                pd_data['ma_short'].shift(1) < pd_data['ma_long'].shift(1)), 'ma_signal'] = 1
        # 死叉
        pd_data.loc[(pd_data['ma_short'] <= pd_data['ma_long']) & (
                pd_data['ma_short'].shift(1) > pd_data['ma_long'].shift(1)), 'ma_signal'] = -1

        pd_data['ma_signal'].fillna(value=0, inplace=True)
    #     ta.
    #
    # def
